package com.weiaibenpao.demo.chislim.ui.marathon;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.adater.MyGameAdapter;
import com.weiaibenpao.demo.chislim.bean.MyGameBean;
import com.weiaibenpao.demo.chislim.bean.UserBean;
import com.weiaibenpao.demo.chislim.model.MyModel;
import com.weiaibenpao.demo.chislim.sport.SportRecordShareActivity;
import com.weiaibenpao.demo.chislim.ui.MatchDetailActivity;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyGameActivity extends AppCompatActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.recycler_view_marathon)
    PullLoadMoreRecyclerView recyclerViewMarathon;
    private MyGameAdapter myGameAdapter;
    private int page;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_game);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.blackbg), 0);
        ButterKnife.bind(this);
        initView();

    }

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, MyGameActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        page = 0;
        initData();
    }

    private void initData() {
        MyModel.getModel().getService().mineActivity(UserBean.getUserBean().userId, page + "", "20").enqueue(new Callback<MyGameBean>() {
            @Override
            public void onResponse(Call<MyGameBean> call, Response<MyGameBean> response) {
                recyclerViewMarathon.setPullLoadMoreCompleted();
                if (response.body() != null) {
                    MyGameBean.DataBean data = response.body().getData();
                    Log.e("qlxxx", "onResponse: " + new Gson().toJson(response.body()));
                    if (response.body().getCode() == 0 && data != null) {

                        if (page == 0) {
                            Log.e("qlxxx", "page: " + page);
                            myGameAdapter.clear();
                        }
                        myGameAdapter.addDate(data.getList());
                    }
                }
            }

            @Override
            public void onFailure(Call<MyGameBean> call, Throwable throwable) {
                recyclerViewMarathon.setPullLoadMoreCompleted();
                Toast.makeText(MyGameActivity.this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void initView() {
        myGameAdapter = new MyGameAdapter(this);
        recyclerViewMarathon.setAdapter(myGameAdapter);
        recyclerViewMarathon.setLinearLayout();

        recyclerViewMarathon.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                page = 0;
                initData();
            }

            @Override
            public void onLoadMore() {
                page++;
                initData();
            }
        });
        myGameAdapter.setOnItemClick(new MyGameAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(MyGameBean.DataBean.ListBean listBean) {
                if (listBean.getDmessage().equals("已完赛")) {
                    if (!TextUtils.isEmpty(listBean.getResult())) {
                        SportRecordShareActivity.openActivity(MyGameActivity.this, listBean.getResult(), listBean.getMarathonTitle(), true, true);
                    }
                } else {
                    MatchDetailActivity.openActivity(MyGameActivity.this, listBean.getId());

                }
            }
        });
    }

    @OnClick(R.id.back)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
